package com.yjzx.care.base;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author yjzx
 * @date 2023/12/28
 * @description 循环基类
 */
public abstract class BaseLoop implements Loop {
    /**
     * 循环控制
     */
    private final AtomicBoolean CONTROL = new AtomicBoolean(true);
    /**
     * 循环对象禁止重复利用
     */
    private final AtomicBoolean IS_LOOP = new AtomicBoolean(false);


    /**
     * 判断循环是否结束
     *
     * @return 状态
     */
    @Override
    public boolean isBreak() {
        return CONTROL.get();
    }

    /**
     * 结束循环
     */
    @Override
    public BaseLoop toBreak() {
        CONTROL.set(false);
        return this;
    }

    /**
     * 循环是否使用
     */

    protected void isLoop() {
        if (IS_LOOP.getAndSet(true)||!isBreak()) {
            throw new IllegalStateException("loop can only be used once.");
        }
    }
}
